package org.application.bean;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.application.model.Paciente;

@RequestScoped
public class PacienteDao {

	@DataRepository
	@Inject
	private EntityManager entityManager;
	
	public EntityManager getEntityManager() {
		return entityManager;
	}

	@SuppressWarnings("unchecked")
	public List<Paciente> getAll() {
		return this.entityManager.createQuery("select distinct p from Paciente p").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Paciente> getByNome(String name) {
		return this.entityManager.createQuery("select distinct p from Paciente p where p.nome like '%:name%'").setParameter("name", name).getResultList();
	}
	
	public Paciente get(Long userId) {
		return this.entityManager.find(Paciente.class, userId);
	}

	public void save(Paciente object) {
		this.entityManager.getTransaction().begin();
		this.entityManager.persist(object);
		this.entityManager.getTransaction().commit();
	}
	
	public void delete(Paciente object){
		this.entityManager.getTransaction().begin();
		this.entityManager.remove(object);
		this.entityManager.getTransaction().commit();
	}
	
	public void update(Paciente object){
		this.entityManager.getTransaction().begin();
		this.entityManager.merge(object);
		this.entityManager.getTransaction().commit();
	}
}
